package net.cyue.web.easyquery.springboot;

import net.cyue.util.ResourceUtil;
import net.cyue.web.easyquery.core.EasyQueryApplication;
import net.cyue.web.easyquery.core.EasyQueryApplicationFactory;
import net.cyue.web.easyquery.core.EasyQueryContext;
import net.cyue.web.easyquery.core.config.ConfigException;
import net.cyue.web.easyquery.core.config.json.EasyQueryJSONConfiguration;
import net.cyue.web.easyquery.core.config.toml.EasyQueryTOMLConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.servlet.ServletContext;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

@Configuration
@ComponentScan(basePackages = "net.cyue.web.easyquery")
public class EasyQueryApplicationConfiguration {

    private final Logger logger = LoggerFactory.getLogger(this.toString());
    private EasyQueryApplication<ServletContext> app;
    private final String[] tomlFilePathArray;
    private final String[] jsonFilePathArray;
    private final String[] propertiesPathArray;

    @Autowired
    public EasyQueryApplicationConfiguration(EasyQueryApplicationConfigurationContainer container) {
        this.logger.info("Configuration init...");
        this.tomlFilePathArray = container.getTOMLFilePathArray();
        this.jsonFilePathArray = container.getJSONFilePathArray();
        this.propertiesPathArray = container.getPropertiesPathArray();
        this.logger.info("tomlFiles: {}", Arrays.toString(this.tomlFilePathArray));
        this.logger.info("jsonFiles: {}", Arrays.toString(this.jsonFilePathArray));
        this.logger.info("properties: {}", Arrays.toString(this.propertiesPathArray));
    }

    @Bean({"EasyQueryApplication", "easyQueryApplication"})
    public EasyQueryApplication<ServletContext> easyQueryApplication(ServletContext context)
        throws ConfigException, IOException
    {
        this.logger.info("EasyQueryApplication init...");
        this.app = EasyQueryApplicationFactory.create(context);
        for(String path : this.tomlFilePathArray) {
            InputStream configInputStream = ResourceUtil.getResourceAsStream(path);
            if (configInputStream == null) {
                this.logger.warn("{} not found", path);
                continue;
            }
            this.logger.info("{} run toml: {}", this.app, path);
            this.app.run(EasyQueryTOMLConfiguration.fromStream(configInputStream));
            configInputStream.close();
        }
        for (String path : this.jsonFilePathArray) {
            InputStream configInputStream = ResourceUtil.getResourceAsStream(path);
            if (configInputStream == null) {
                this.logger.warn("{} not found", path);
                continue;
            }
            this.logger.info("{} run json: {}", this.app, path);
            this.app.run(EasyQueryJSONConfiguration.fromStream(configInputStream));
            configInputStream.close();
        }
        for (String path : this.propertiesPathArray) {
            InputStream configInputStream = ResourceUtil.getResourceAsStream(path);
            if (configInputStream == null) {
                this.logger.warn("{} not found", path);
                continue;
            }
            this.logger.info("{} run properties: {}", this.app, path);
            this.app.runByProperties(configInputStream);
            configInputStream.close();
        }
        return this.app;
    }

    @Bean({"EasyQueryContext", "easyQueryContext"})
    public EasyQueryContext<ServletContext> easyQueryContext(EasyQueryApplication<ServletContext> app) {
        return app.getContext();
    }
}
